package com.mastercard.labs

import org.codehaus.groovy.grails.validation.routines.UrlValidator

class Voucher {

    static constraints = {
		title nullable: false, blank: false, unique: true, maxSize: 100
		imageUrl nullable: true, maxSize: 255, validator: {UrlValidator urlValidator = new UrlValidator(); if (it == null || it == "" || urlValidator.isValid(it) || urlValidator.isValid("http://" + it)) return true; else return false;}
		description nullable: true, maxSize: 255
		country nullable: false
		merchant nullable: true
		categories nullable: true
		rdptPoint nullable: false, blank: false, maxSize: 15, validator: { val, obj -> val.toInteger() > 0}
		totalQuantity nullable: false, blank: false, maxSize: 10, validator: { val, obj -> val.toInteger() >= 0}
		//startDate nullable: false, validator: {it > new Date()-1}
		startDate nullable: false, validator: { val, obj -> log.info 'startdate:' +val +' with id:' + obj.id; if (val) {if (obj.id == null) {return val > new Date()-1} else { true}}} //Date oriDate =    Date.parse( "yyyy-MM-dd", params.dbStartDate ); log.info 'oriDate:' + oriDate; return !oriDate.after(val) }
		endDate nullable: false, validator: { val, obj -> if (val && obj.startDate) !val?.before(obj.startDate)}

    }
	String title
	String imageUrl
	String description
	Merchant merchant
	List categories
//	String location
	Country country
	int rdptPoint
	Date startDate
	Date endDate
	int totalQuantity
	
	static hasMany=[categories:Category]
	
}
